package com.atguigu.gmall.activity.service.impl;

import com.atguigu.gmall.activity.model.SeckillGoods;
import com.atguigu.gmall.activity.mapper.SeckillGoodsMapper;
import com.atguigu.gmall.activity.service.SeckillGoodsService;
import com.atguigu.gmall.common.constant.RedisConst;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundHashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 *  业务实现类
 *
 * @author atguigu
 * @since 2023-05-14
 */
@Service
public class SeckillGoodsServiceImpl extends ServiceImpl<SeckillGoodsMapper, SeckillGoods> implements SeckillGoodsService {

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 查询Redis中当日秒杀商品列表
     * @return
     */
    @Override
    public List<SeckillGoods> getSeckillGoodsList() {
        String seckillGoodsKey = RedisConst.SECKILL_GOODS;
        BoundHashOperations<String,String,SeckillGoods> hashOps = redisTemplate.boundHashOps(seckillGoodsKey);
        return hashOps.values();
    }

    /**
     * 查询指定秒杀商品信息
     * @param skuId
     * @return
     */
    @Override
    public SeckillGoods getSeckillGoods(Long skuId) {
        String seckillGoodsKey = RedisConst.SECKILL_GOODS;
        BoundHashOperations<String,String,SeckillGoods> hashOps = redisTemplate.boundHashOps(seckillGoodsKey);
        return hashOps.get(skuId.toString());
    }
}
